package com.gs.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gs.dao.DAO;
import com.liang.util.Page;


public abstract class DAOBaseImpl<Model> extends HibernateDaoSupport implements
		DAO<Model> {

	/**
	 * 当前模型实体类对象
	 */
	private Class<Model> modelClazz;

	public DAOBaseImpl() {
		Type genType = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		modelClazz = (Class) params[0];
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
	public Model save(Model model) throws Exception {
		getHibernateTemplate().save(model);
		return model;
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
	public void delete(Model model) throws Exception {
		getHibernateTemplate().delete(model);
		
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
	public Model updata(Model model) throws Exception {
		getHibernateTemplate().update(model);
		return model;
	}

	@SuppressWarnings("unchecked")
	public Model find(long id) throws Exception {
		return (Model)getHibernateTemplate().get(modelClazz, id);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
	public Model delete(long id) throws Exception {
		Model model = find(id);
		getHibernateTemplate().delete(model);
		return model;
	}

	public abstract Page<Model> queryPage(int page, int pageSize,
			Model searchModel) throws Exception;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.manager.Manager#queryCount(java.lang.String)
	 */
	public Object query(Object paras) throws Exception {
		return null;
	}

}
